using DA.AnimGraph;
using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {

    internal class OpenGraphService : Service {
        /// <summary>
        /// 供 HierarchyPane 使用，其它调用源请调用 HierarchyPaneService.OpenGraphByUuid
        /// </summary>
        public void OpenGraph(AUID uuid) {
            (provider as EditingFramePane).OpenGraph(uuid);
        }
    }


    internal class EditingFramePane : ContextPaneBase {

        HandleKeeper<AGAHandle_Root> rootHK;

        private VisualElement leftContainer;
        private VisualElement centerContainer;
        private VisualElement rightContainer;

        HierarchyPane hierarchyPane;
        BlackboardPane blackboardPane;
        ResourcesPane resourcesPane;
        GraphPane graphPane;
        InspectorPane inspectorPane;

        internal EditingFramePane(AGEEnvironment ageEnv) : base(ageEnv) {
            BuildLayout();
        }

        protected override void OnSetHandle(AGAHandleBase handle) {
            Debug.Assert(handle is AGAHandle_Root || handle == null);
            if (handle == null) {
                rootHK = default;
                return;
            }
            rootHK = new HandleKeeper<AGAHandle_Root>(handle as AGAHandle_Root);
        }

        protected override void OnClearUI() {
            ageContext.serviceManager.RemoveService<OpenGraphService>(this);
            hierarchyPane?.SetHandle(null);
            graphPane?.SetHandle(null);
            inspectorPane?.SetHandle(null);
            Clear();
        }

        void BuildLayout() {
            hierarchyPane = new HierarchyPane(this) {
                name = "hierarchy_pane",
                style = {
                    //flexGrow = 1,
                    minHeight = Length.Percent(20),
                }
            };

            blackboardPane = new BlackboardPane(this) {
                name = "blackboard_pane",
                style = {
                    flexGrow = 1,
                    minHeight = Length.Percent(20),
                }
            };

            resourcesPane = new ResourcesPane(this) {
                name = "resources_pane",
                style = {
                    minHeight = Length.Percent(20),
                }
            };

            graphPane = new GraphPane(this) {
                name = "graph_pane",
                style = { }
            };

            inspectorPane = new InspectorPane(this) {
                name = "inspector_pane",
                style = { }
            };
            
            // layout
            // 先纵向分行
            // style.flexDirection = FlexDirection.Column;

            // 再横向分列
            style.flexDirection = FlexDirection.Row;

            leftContainer = new VisualElement() {
                name = "left_container",
                style = {
                    flexDirection = FlexDirection.Column, // 内部纵向
                    //flexGrow = 1,
                    minWidth = 300, // Length.Percent(10),
                }
            };
            centerContainer = new VisualElement() {
                name = "center_container",
                style = {
                    flexDirection = FlexDirection.Column, // 内部纵向
                    flexGrow = 1,
                }
            };
            rightContainer = new VisualElement() {
                name = "right_container",
                style = {
                    flexDirection = FlexDirection.Column,
                    //flexGrow = 1,
                    minWidth = 300, // Length.Percent(10),
                }
            };

        }

        protected override void OnDrawUI() {
            ageContext.serviceManager.AddService<OpenGraphService>(this);
            DrawLayout();
            // SetHandle会导致子元素绘制，在最后执行
            blackboardPane.SetHandle(handle);
            resourcesPane.SetHandle(handle);
            inspectorPane.SetHandle(handle);
            hierarchyPane.SetHandle(handle);
        }

        void DrawLayout() {
            Add(leftContainer);
            Add(new PaneSplitter(leftContainer, PaneSplitter.Direction.Row, 1));
            Add(centerContainer);
            Add(new PaneSplitter(rightContainer, PaneSplitter.Direction.Row, -1));
            Add(rightContainer);

            // 左侧
            {
                var splitter1 = new PaneSplitter(hierarchyPane, PaneSplitter.Direction.Column, 1);
                //var splitter2 = new PaneSplitter(resourcesPane, PaneSplitter.Direction.Column, -1);
                leftContainer.Add(hierarchyPane);
                leftContainer.Add(splitter1);
                leftContainer.Add(blackboardPane);
                //leftContainer.Add(splitter2);
                //leftContainer.Add(resourcesPane);
            }
            // 中间
            {
                centerContainer.Add(graphPane);
                graphPane.StretchToParentSize();
            }
            // 右侧
            {
                rightContainer.Add(inspectorPane);
                inspectorPane.StretchToParentSize();
            }
        }

        public void OpenGraph(AUID uuid) {
            //Debug.Log($"open {uuid}");
            graphPane.SetHandleFromParent(uuid);
        }
    }


}
